package cn.yapin.gzh.action;

import cn.yapin.gzh.entity.UserInfo;
import cn.yapin.gzh.service.IUserInfo;
import cn.yapin.gzh.utils.WebUtils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SellersManagerAction extends BaseAction {
    private UserInfo userInfo;
    private IUserInfo userInfoService;

    public void setUserInfoService(IUserInfo userInfoService) {
        this.userInfoService = userInfoService;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public String login() {
        if (userInfo != null) {
            try {
                String decodePwd = new String(WebUtils.decode(userInfo.getPassword()));
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(decodePwd.getBytes());
                userInfo.setPassword(new BigInteger(md.digest()).toString(32));
                UserInfo findUserInfo = userInfoService.loadUser(userInfo);
                if (findUserInfo != null) {
                    respJSON.put("userId", findUserInfo.getId());
                    respJSON.put("deliverRegion", findUserInfo.getDeliverRegion());
                    respJSON.put("success", true);
                    return SUCCESS;
                }
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        respJSON.put("success", false);
        return ERROR;
    }
}
